
//
//  SC_ChildViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Gcx. All rights reserved.
//



#import "SC_ChildViewController.h"
#import "SC_NavigationController.h"

@interface SC_ChildViewController ()

@end

@implementation SC_ChildViewController

- (void)dealloc
{
    if (self)
    {
        //取消延迟执行
        [[SC_ChildViewController class] cancelPreviousPerformRequestsWithTarget:self];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];


    SC_NavBarView *navbar = [[SC_NavBarView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [SC_NavBarView barSize].width, [SC_NavBarView barSize].height)];
    _navbar = navbar;
    _navbar.viewCtrlParent = self;
    [self.view addSubview:_navbar];
    
    //
    [self navigationCanDragBack:YES];

    //设置导航栏背景颜色
    [self setNabbarBackgroundColor:Color1a97e9];
    
    //TODO:
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 0) {
            [self setNavBarLeftBtn:nil];
        }
    }
    
    

}

//TODO:
-(void)setTitle:(NSString *)title{
    
    [super setTitle:title];
    
    [self setNavBarTitle:title];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (_navbar && !_navbar.hidden)
    {
        [self.view bringSubviewToFront:_navbar];
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

#pragma mark -
- (void)bringNavBarToTopmost
{
    if (_navbar)
    {
        [self.view bringSubviewToFront:_navbar];
    }
}


// 设置NavBarView 颜色
- (void)setNabbarBackgroundColor:(UIColor *)color
{
    if (_navbar) {
        [_navbar setBackgroundColor:color];
    }
}

- (void)hideNavBar:(BOOL)bIsHide
{
    _navbar.hidden = bIsHide;
}

- (void)setNavBarTitle:(NSString *)strTitle
{
    if (_navbar)
    {
        [_navbar setTitle:strTitle];
    }
    else{
        NSLog(@"APP_ASSERT_STOP");
        NSAssert1(NO, @" \n\n\n===== APP Assert. =====\n%s\n\n\n", __PRETTY_FUNCTION__);
    }
}


- (void)setNavBarLeftBtn:(UIButton *)btn
{
    if (_navbar)
    {
        [_navbar setLeftBtn:btn];
    }
    else{
        NSLog(@"APP_ASSERT_STOP");
        NSAssert1(NO, @" \n\n\n===== APP Assert. =====\n%s\n\n\n", __PRETTY_FUNCTION__);
    }
}

- (void)setNavBarRightBtn:(UIButton *)btn
{
    if (_navbar)
    {
        [_navbar setRightBtn:btn];
    }
}


// 是否可右滑返回
- (void)navigationCanDragBack:(BOOL)canDragBack
{
    if (self.navigationController)
    {
        [((SC_NavigationController *)(self.navigationController)) navigationCanDragBack:canDragBack];
    }
}


// 重设scroll view的内容区域和滚动条区域
- (void)resetScrollView:(UIScrollView *)scrollView tabBar:(BOOL)hasTabBar
{
    if (scrollView)
    {
        UIEdgeInsets inset = scrollView.contentInset;
        UIEdgeInsets insetIndicator = scrollView.scrollIndicatorInsets;
        
        CGPoint ptContentOffset = scrollView.contentOffset;
        CGFloat fTopInset = 0.0f;
        CGFloat fTopIndicatorInset = 0.0f;
        
        CGFloat fBottomInset = hasTabBar ? Bottom_H : 0.0f;
        
        fTopInset += NaviBar_H - StatusBar_H;
        fTopIndicatorInset += NaviBar_H - StatusBar_H;
        
        inset.top += fTopInset;
        inset.bottom += fBottomInset;
        [scrollView setContentInset:inset];
        
        insetIndicator.top += fTopIndicatorInset;
        insetIndicator.bottom += fBottomInset;
        [scrollView setScrollIndicatorInsets:insetIndicator];
        
        ptContentOffset.y -= fTopInset;
        [scrollView setContentOffset:ptContentOffset];
    }
}

- (BOOL)checkTel:(NSString *)str

{
    if ([str length] == 0) {
        [[JLActionManager sharedInstance]showHUDWithLabelText:@"电话号码不能为空" imageView:FailImage inView:self.view];
        
        return NO;
    }
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,4-9]))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        [[JLActionManager sharedInstance]showHUDWithLabelText:@"请输入正确的手机号码" imageView:FailImage inView:self.view];
        return NO;
    }

    return YES;
    
}


//取userDefaults里面的值
- (NSString *)userDefaults:(NSString *)key{
    
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    
    NSString *value = [userDefaultes stringForKey:key];
    
    return value;
}

- (BOOL) checkCardNo:(NSString*) cardNo{
    int oddsum = 0;     //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
    
    cardNo = [cardNo substringToIndex:cardNoLength - 1];
    for (int i = cardNoLength -1 ; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1 ) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}
//16进制颜色转换
-(UIColor *)getColor:(NSString *)hexColor {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}
/**
 
 设置图片的透明度转换
 
 
 */
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha  image:(UIImage*)image

{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, alpha);
    CGContextDrawImage(ctx, area, image.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    
}

/**
 
 *用color生成图片
 
 
 */

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

-(BOOL) isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}




- (void)removecookies{
    
    //清空WebView缓存 Sunny 以便下个用户再次登陆
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
}


//邮箱的正则表达式
-(BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
//手机的手机正则表达
- (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}


//时间戳转换
- (NSString *)since1970String:(NSString *)data
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[data intValue]];
    NSString * nowtimeStr = [formatter stringFromDate:date];
    
    return nowtimeStr;
}


- (NSString *)escapeCharacter:(NSString *)string{
    
    NSString *str1 = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
        
    NSString *str3 = [str1 stringByReplacingOccurrencesOfString:@"\"" withString:@"&quot;"];
    
    NSString *str4 = [str3 stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
    
    NSString *str5 = [str4 stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
    
    return str5;
    
}

- (NSInteger)sc_currentPageNumWithTotalCount:(NSInteger)total{
    
    NSInteger _page_size = 10;
    
    NSInteger pindex = total / _page_size;
    
    //如果有余数, 则在上方除10的时候把余数丢掉.下方进行判断, 如果有余数, 则+1处理
    if (total % _page_size) {
        pindex += 1;
    }
    return pindex + 1;
    
}

@end
